/**
 * 充值道具列表
 */

var item = new Object({
    table:null,
    init:function(){
        if(null != this.table)
            return;
        this.queryInfo();
        this.bindEvent();
    },
    queryInfo:function(){
        this.table = $('#tb_item_list').bootstrapTable({
            url: contextPath + '/item/listData.json',
            responseHandler: function (res) {
                return res.data;
            },
            queryParams: function (params) {
                return params;
            },
            pagination: true,
            pageList: '[10,20,50,100]',
            //showColumns: true,
            showRefresh: true,
            showToggle: true,
            //showPaginationSwitch:true,
            sidePagination: 'server',
            //striped:true,
            sortable: false,
            height: 700,
            search: false,
            searchAlign: 'left',
            toolbar: '<div style="float: left;margin-right:5px;">' +
            '    <button class="btn btn-default" type="button" onclick="item.add()">' +
            '        <i style="margin-right:2px;" class="fa fa-search"></i>' +
            '        <span>新增</span>' +
            '    </button>' +
            '</div>',
            idField: 'id',
            uniqueId: 'id',
            //singleSelect: true,
            clickToSelect: true,
            columns: [{
                field: 'name',
                title: '道具名称',
                width: 100
            }, {
                field: 'money',
                title: '道具金额(元)',
                width: 100
            }, {
                field: 'payNum',
                title: '购买数量',
                width: 100
            }, {
                field: 'giveNum',
                title: '赠送数量',
                width: 100
            }, {
                field: 'firstRechargeGive',
                title: '首冲赠送',
                width: 100
            }, {
                field: 'type',
                title: '道具类型',
                width: 100,
                formatter:function(value,row,index){
                    if(value == 1){
                        return '基础类型'
                    } else if(value == 2){
                        return '充值类型'
                    }
                }
            }, {
                field: 'description',
                title: '描述',
                width: 100
            },{
                field: 'op',
                title: '操作',
                align: 'center',
                width: 100,
                formatter: function (value, row, index) {
                    if(row.type == 1) return "";
                    var strHtml = '<div class="btn-group">' +
                        '<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">更多<i class="fa fa-caret-down"></i></button>' +
                        '   <ul class="dropdown-menu">'+
                        '       <li><a href="javascript:void(0);" onclick="item.editItem('+row.id+')">编辑</a></li>'+
                        '       <li><a href="javascript:void(0);" onclick="item.deleteItem('+row.id+')">删除</a></li>'+
                        '   </ul>'+
                        '</div>'
                    return strHtml;
                }
            }]
        });
        $('.search input').attr("placeholder", "类型");
    },
    bindEvent: function () {
    },
    add:function(){
        var addItemModal = '<div id="addItemModal" class="modal fade" tabindex="-1" role="dialog" style="display: none;">'+
            '   <div class="modal-dialog">'+
            '       <div class="modal-content">'+
            '           <div class="modal-header">'+
            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'+
            '               <h4 class="modal-title" id="setWxInfoModalLabel">创建道具</h4>'+
            '           </div>'+
            '           <div class="modal-body">'+
            '               <form class="form-horizontal" id="addItemForm">'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">名称：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="name" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">金额：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="money" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">购买数量：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="payNum"/>'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">赠送数量：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="giveNum"/>'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">首冲赠送数量：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="firstRechargeGive"/>'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">描述：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="description"/>'+
            '                       </div>'+
            '                   </div>'+
            '               </form>'+
            '           <div class="modal-footer">' +
            '               <center>' +
            '                   <button class="btn btn-primary" onclick="item.addSubmit()" >保存</button>' +
            '                   <button class="btn btn-primary" type="button" data-dismiss="modal" aria-hidden="true">关闭</button>' +
            '               </center>' +
            '           </div>'+
            '       </div>'+
            '   </div>'+
            '</div>';

        /**
         * 添加html中，隐藏
         */
        document.body.appendChild($(addItemModal)[0]);
        $('#addItemModal').on('hidden.bs.modal', function () {
            document.body.removeChild($('#addItemModal')[0]);
        });
        item.bindEvent();
        //显示
        $("#addItemModal").modal('show')
    },
    addSubmit:function(){
        $.ajax({
            url:contextPath+'/item/add',
            method:'post',
            data:$('#addItemForm').serialize(),
            success:function(res){
                item.table.bootstrapTable('refresh');
            },
            error: function () {
                alert_message("添加出错，请联系管理员")
            }
        });
        item.closeAddModal();
    },
    closeAddModal:function(){
        $('#addItemModal').modal('hide');
    },
    editItem:function(id){
        var res = "";
        $.ajax({
            url:contextPath+'/item/'+id,
            method:'get',
            async:false,
            success:function(data){
                //获取数据
                res = data;
            },
            error:function(){
                alert_message('请求错误，请联系管理员');
            }
        });
        var editItemModal = '<div id="editItemModal" class="modal fade" tabindex="-1" role="dialog" style="display: none;">'+
            '   <div class="modal-dialog">'+
            '       <div class="modal-content">'+
            '           <div class="modal-header">'+
            '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>'+
            '               <h4 class="modal-title" id="setWxInfoModalLabel">编辑道具</h4>'+
            '           </div>'+
            '           <div class="modal-body">'+
            '               <form class="form-horizontal" id="editItemForm">'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">名称：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="name" value="'+res.data.name+'"/>'+
            '                           <input type="hidden" class="form-control" name="id" value="'+res.data.id+'" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">金额：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="money" value="'+res.data.money+'" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">购买数量：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="payNum" value="'+res.data.payNum+'" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">赠送数量：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="giveNum" value="'+res.data.giveNum+'" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">首冲赠送：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="firstRechargeGive" value="'+res.data.firstRechargeGive+'" />'+
            '                       </div>'+
            '                   </div>'+
            '                   <div class="form-group">'+
            '                       <label class="col-sm-3 control-label">描述：</label>'+
            '                       <div class="col-sm-9">'+
            '                           <input type="text" class="form-control" name="description" value="'+res.data.description+'" />'+
            '                       </div>'+
            '                   </div>'+
            '               </form>'+
            '           <div class="modal-footer">' +
            '               <center>' +
            '                   <button class="btn btn-primary" onclick="item.editItemSubmit()" >保存</button>' +
            '                   <button class="btn btn-primary" type="button" data-dismiss="modal" aria-hidden="true">关闭</button>' +
            '               </center>' +
            '           </div>'+
            '       </div>'+
            '   </div>'+
            '</div>';

        //alert(editItemModal);

        //显示
        /**
         * 添加html中，隐藏
         */
        document.body.appendChild($(editItemModal)[0]);
        $('#editItemModal').on('hidden.bs.modal', function () {
            document.body.removeChild($('#editItemModal')[0]);
        });

        //绑定事件
        item.bindEvent();

        //显示
        $('#editItemModal').modal('show');


    },
    editItemSubmit:function(){
        $.ajax({
            url:contextPath+'/item/update',
            data:$('#editItemForm').serialize(),
            method:'post',
            success:function(res){
                alert_message("修改成功");
                item.table.bootstrapTable('refresh');
                $('#editItemModal').modal('hide');
            },
            error:function(){
                alert_message("修改出错，请联系管理员");
            }
        });
    },
    deleteItem:function(id){
        bootbox.confirm('确认删除吗？',function(result){
                if(result) {
                    $.ajax({
                        url: contextPath + '/item/delete' ,
                        method: 'post',
                        data:{id:id},
                        success: function (res) {
                            item.table.bootstrapTable('refresh');
                            alert_message("删除成功");
                        },
                        error: function () {
                            alert_message("删除出错，请联系管理员");
                        }
                    });
                }
            }
        );
    }
});